package com.itbour.web.action.material;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.log.ErrorLogger;
import com.itbour.common.util.StringUtils;
import com.itbour.def.MessageCode;
import com.itbour.def.MessageCodeConstants;
import com.itbour.def.MessageType;
import com.itbour.def.SystemConf;
import com.itbour.dto.com.GraphInfoDto;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.service.material.GraphService;
import com.opensymphony.xwork2.ActionContext;

/**
 * copyright Itbour team 2015
 * 
 * @author xc
 */

public class GetGraphListWebAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private GraphService graphService;
	private List<GraphInfoDto> graphInfoList;
	private GraphInfoDto graphInfo;
	private String inputData;// 用户输入的json字符串(数组格式):"[{GraphInfoDto},{GraphInfoDto}]"
	private String graph_id;
	private String graph_name;
	private String delIds;
	private String findName;// 查询用
	
	HashMap<String, Object> mapResult = new HashMap<String, Object>(); 

	public String getGraphData() throws Exception {

		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> param = new HashMap<String, Object>();
		String id = request.getParameter("graph_id");
		if (StringUtils.isNotEmpty(id)) {
			param.put("graph_id", id);
		}

		graphInfoList = graphService.selectGraphInfo(param);
		return SUCCESS;
	}
	
	public String convert(String target) {  
        try {  
            return new String(target.trim().getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return target;  
        }  
    } 
	
	public String getGraphList() throws Exception {
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	
    	HttpServletRequest request = ServletActionContext.getRequest();
		String pageNumber = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		String getAll = request.getParameter("all");
		Map<String, Object> param = new HashMap<String, Object>();
		if (!StringUtils.isEqual(getAll, "1")
				&& (user!=null && user.getUser_id()!=null && !user.isSuperUser()) ) {
			param.put("user_id", user.getUser_id());// 默认检索自己上传的图形
		}
		if (StringUtils.isNotEmpty(findName)) {
			param.put("graph_name", "%"+findName+"%");
		}
		Page page = graphService.pageQuery(param, Integer.parseInt(pageNumber),
				Integer.parseInt(pageSize));
		HashMap<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("total", page.getTotalCount());
		resMap.put("rows", page.getResult());
		//writeJSON(resMap);
		mapResult = resMap;
		
		return SUCCESS;
	}
	
	public void deleteGraph() throws Exception {
		graphService.deleteGraphInfo(delIds);
		this.writeJSON("ok");
	}

	public void addGraph() throws Exception {
/*		Long retId = null;
		if (graphInfo != null) {
			Map<String, Object> session = ActionContext.getContext()
					.getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	if (user != null) {
	    		graphInfo.setUpdate_by(user.getUser_name());
	    		graphInfo.setUser_id(user.getUser_id());
	    		graphInfo.setUser_type(user.getUser_type());
	    	}
	    	graphService.addGraph(graphInfo);
	    	if (graphInfo.getGraph_id() != null && graphInfo.getGraph_id()>=0){
	    		retId = graphInfo.getGraph_id();
	    	}
		}
		//this.writeJSON("ok");
		HashMap<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("retId", retId);
		this.writeJSON(retMap);*/
		
		List<Long> retIds = new ArrayList<Long>();
		if (inputData != null && inputData.length()>0) {
			Gson gson = new Gson();
			List<GraphInfoDto> lst = gson.fromJson(inputData, new TypeToken<List<GraphInfoDto>>(){}.getType());  
			if (lst != null) {
				Map<String, Object> session = ActionContext.getContext().getSession();
		    	UserInfoDto user = (UserInfoDto)session.get("user");
		    	
				for (int i=0; i<lst.size(); i++) {
					GraphInfoDto dto = lst.get(i);
					if (user != null) {
						dto.setUpdate_by(user.getUser_name());
						dto.setUser_id(user.getUser_id());
						dto.setUser_type(user.getUser_type());
			    	}
					graphService.addGraph(dto);
					if (dto.getGraph_id() != null && dto.getGraph_id()>=0){
						retIds.add(dto.getGraph_id());
			    	}
				}
			}
		}
		//this.writeJSON("ok");
		HashMap<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("retIds", retIds);
		this.writeJSON(retMap);
	}

	public GraphService getGraphService() {
		return graphService;
	}

	public void setGraphService(GraphService graphService) {
		this.graphService = graphService;
	}

	public List<GraphInfoDto> getGraphInfoList() {
		return graphInfoList;
	}

	public void setGraphInfoList(List<GraphInfoDto> graphInfoList) {
		this.graphInfoList = graphInfoList;
	}

	public String getGraph_name() {
		return graph_name;
	}

	public void setGraph_name(String graph_name) {
		this.graph_name = graph_name;
	}

	public String getDelIds() {
		return delIds;
	}

	public void setDelIds(String delIds) {
		this.delIds = delIds;
	}

	public String getFindName() {
		return findName;
	}

	public void setFindName(String findName) {
		this.findName = findName;
	}

	public HashMap<String, Object> getMapResult() {
		return mapResult;
	}

	public void setMapResult(HashMap<String, Object> mapResult) {
		this.mapResult = mapResult;
	}

	public GraphInfoDto getGraphInfo() {
		return graphInfo;
	}

	public void setGraphInfo(GraphInfoDto graphInfo) {
		this.graphInfo = graphInfo;
	}

	public String getInputData() {
		return inputData;
	}

	public void setInputData(String inputData) {
		this.inputData = inputData;
	}

	public String getGraph_id() {
		return graph_id;
	}

	public void setGraph_id(String graph_id) {
		this.graph_id = graph_id;
	}

}
